import { getCurrentInstance } from '@vue/runtime-core';
/** 格式化时间
 * @param {string | number | date} time 时间
 * @param {?string} cFormat 格式
 * @returns {string} 时间
 */
export function parseTime(
  time: string | Date | number,
  cFormat: string
): string {
  if (!time || (time as string).length === 0) {
    return null;
  }
  if (arguments.length === 0) {
    return null;
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
  let date: Date;
  if (typeof time === 'object') {
    date = time;
  } else {
    if (('' + time).length === 10) {
      time = parseInt(time as string) * 1000;
    }
    if (typeof time === 'string') {
      time = time.replace(/-/g, '/');
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    if (key === 'a')
      return ['一', '二', '三', '四', '五', '六', '日'][value - 1];
    if (result.length > 0 && value < 10) {
      value = '0' + value;
    }
    return value || 0;
  });
  return time_str;
}

/** 获取挂载到全局的属性
 * @returns {Object} globalProperties
 */
export function globalProperties(): Record<string, any> {
  return getCurrentInstance()?.appContext?.config?.globalProperties;
}

/**
 * @description: 封装sessionStorage和localStorage
 * @param {*}
 * @return {*}
 */
export const storage = {
  /** 设置本地缓存
   * @param {string} key 本地缓存key
   * @param {any} value 本地缓存值
   * @return {boolean} 是否设置成功
   */
  setLocal: (key: string, value: any): boolean => {
    try {
      window.localStorage.setItem(key, JSON.stringify(value));
      return true;
    } catch {
      return false;
    }
  },
  /** 获取本地缓存
   * @param {string} key 本地缓存key
   * @return {any} 值
   */
  getLocal: (key: string): any => {
    try {
      return JSON.parse(window.localStorage.getItem(key));
    } catch {
      return window.localStorage.getItem(key);
    }
  },
  /** 删除指定的某个本地缓存
   * @param {string} key 本地缓存key
   */
  deleteLocal: (key: string) => {
    window.localStorage.removeItem(key);
  },
  /** 清除所有本地缓存
   */
  clearLocal: () => {
    window.localStorage.clear();
  },
  /** 设置会话缓存
   * @param {string} key 会话缓存key
   * @param {any} value 会话缓存值
   * @return {boolean} 是否设置成功
   */
  setSession(key: string, value: any): boolean {
    try {
      window.sessionStorage.setItem(key, JSON.stringify(value));
      return true;
    } catch {
      return false;
    }
  },
  /** 获取会话缓存
   * @param {string} key 会话缓存key
   * @return {any} 值
   */
  getSession: (key: string): any => {
    const value: any = window.sessionStorage.getItem(key);
    if (!value) return value;
    try {
      return JSON.parse(value);
    } catch {
      return value;
    }
  },
  /** 删除指定的某个会话缓存
   * @param {string} key 会话缓存key
   */
  deleteSession: (key: string) => {
    window.sessionStorage.removeItem(key);
  },
  /** 清除所有会话缓存
   */
  clearSession: () => {
    window.sessionStorage.clear();
  },
};

/**
 * @description: 获取接口访问地址
 * @return {*} baseUrl
 */

export function getBaseUrl(): string {
  if (storage.getLocal('baseUrl')) {
    return storage.getLocal('baseUrl').replace(/\s+/g, '"');
  }
  if (import.meta.env.NODE_ENV === 'production') {
    const BASE_URL = import.meta.env.VITE_BASE_URL as string;
    return BASE_URL.replace(/'/g, '');
  } else {
    return '/api';
  }
}



